<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="openAdd(1, '0')">添加权限</el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="lists" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="openAdd(2, row.id)">添加</el-button>
              <el-button type="text" @click="openEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加的弹层 -->
      <el-dialog :visible.sync="showDialog" title="弹层标题" close="close">
        <!-- 表单 -->
        <el-form ref="fm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch v-model="formData.enVisible" active-text="启用" active-value="1" inactive-text="不启用" inactive-value="0" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="addPerm">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permisson'
// 导入树形转换方法
import { tranformTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data () {
    return {
      // 权限点数据分类：1. 页面访问权限（父）  2. 页面下功能（按钮）操作权限（子）
      // 后续做权限控制，也是基于以上两种分类权限数据做控制
      lists: [],
      // 表单数据
      showDialog: false,
      formData: {
        enVisible: '0', // '1' 开启  '0' 不开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型  1。页面访问  2。页面下功能
        pid: '' // 添加到哪个节点下  1,0表示页面权限，id表示页面下的功能权限
      },
      // 校验规则
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {

    // 删除权限点
    async delPermission (row) {
      try {
        await this.$confirm(`你确认要删除${row.name}吗？`)
        // 删除之前要判断是否有子节点
        if (row.children && row.children / length > 0) {
          return this.$message.error('该节点下面有子节点，不能删除')
        }
        await delPermission(row.id)
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑的数据回填
    async openEdit (id) {
      this.showDialog = true
      const detail = await getPermissionDetail(id)
      this.formData = detail
    },
    // 添加权限 和 编辑权限点
    async addPerm () {
      try {
        await this.$refs.fm.validate()
        if (this.formData.id) {
          await updatePermission(this.formData)
        } else {
          await addPermission(this.formData)
        }

        this.getList()
        this.showDialog = false
        this.$message.success(`操作：${this.formData.type === 1 ? '页面访问权限' : '页面下按钮权限'}成功！`)
        this.$refs.fm.resetFields()
        console.log('ok')
      } catch (error) {
        console.log(error)
      }
    },
    openAdd (type, pid) {
      // type表示权限分类  pid表示权限父节点
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 获取列表数据
    async getList () {
      const list = await getPermissionList()
      // console.log(list)
      this.lists = tranformTreeData(list)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
